package com.mark.controller.protal;

import com.google.common.collect.Maps;
import com.mark.common.ServerResponse;
import com.mark.converter.OrderForm2OrderDTOConverter;
import com.mark.dto.OrderDTO;
import com.mark.enums.ResultEnum;
import com.mark.exception.SellException;
import com.mark.from.OrderForm;
import com.mark.service.IBuyerService;
import com.mark.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Choisaaaa on 2018/6/22.
 * 买家订单
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private  IBuyerService iBuyerService;

    //1.创建订单
    @PostMapping("/create")
    public ServerResponse<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        OrderDTO createResult = iOrderService.create(orderDTO);
        Map<String,String> resultMap = Maps.newHashMap();
        resultMap.put("orderId",createResult.getOrderId());

        return ServerResponse.createBySuccess(resultMap);
    }

    //2.订单列表
    @GetMapping("/list")
    public ServerResponse<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                               @RequestParam(value = "page" ,defaultValue = "0") Integer page,
                                               @RequestParam(value = "size",defaultValue = "10") Integer size){
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request = PageRequest.of(page,size);
        Page<OrderDTO> orderDTOPage = iOrderService.findList(openid,request);
        return ServerResponse.createBySuccess(orderDTOPage.getContent());
    }

    //3.单个订单详情
    @GetMapping("/detail")
    public ServerResponse<OrderDTO> detail(@RequestParam("openid") String openid,
                                           @RequestParam("orderId") String orderId){
        OrderDTO orderDTO = iBuyerService.findOrderOne(openid,orderId);
        return ServerResponse.createBySuccess(orderDTO);
    }

    //4.取消订单
    @PostMapping("/cancel")
    public ServerResponse cancel(@RequestParam("openid") String openid,
                                 @RequestParam("orderId") String orderId){
        iBuyerService.cancelOrder(openid, orderId);
        return ServerResponse.createBySuccessMessage("取消订单成功");
    }
}
